package com.zhiyou100.test.test01.reflect;

import java.lang.reflect.Field;

/**
 * @author yang
 * @className Demo03Filed
 * @packageName com.zhiyou100.test.test01.reflect
 * @Description TODO 反射练习3 属性练习
 * @date 2020-08-06 11:33
 */
public class Demo03Filed {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("com.zhiyou100.test.test01.reflect.Student");
        /*
        Filed 属性的封装
        String getName()
        class<?> getType()
        Object get(Object 0)
        void set(Object 0,Object v)
        void setAccessible(boolean b) 是否取消对编译器的检查
         */

        // 创建对象
        Object o = aClass.getDeclaredConstructor(int.class).newInstance(1);

        for (Field declaredField : aClass.getDeclaredFields()) {
            // 暴力访问
            declaredField.setAccessible(true);
            System.out.println(declaredField.getName() + " : " + declaredField.getType() + " : " + declaredField.get(o));
        }

        Field s = aClass.getDeclaredField("s");
        s.setAccessible(true);
        System.out.println(o);
        s.set(o,"你好");
        System.out.println(s);
        System.out.println(o);

    }
}